﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;

namespace Comm
{
    /// <summary>
    /// Xml类
    /// </summary>
    public class CustomXmlDocument
    {
        /// <summary>
        /// 将Xml转成 dataset
        /// </summary>
        /// <param name="xmlfilepath">Xml文件全路径</param>
        /// <returns>dataset</returns>
        public static DataSet getDataset(string xmlfilepath)
        {
            bool flag = File.Exists(xmlfilepath);
            if (!flag)
            {
                return null;
            }
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(xmlfilepath);
            }
            catch
            {
                return null;
            }
            return ds;
        }

        /// <summary>
        /// 将Xml转成 datatable
        /// </summary>
        /// <param name="xmlfilepath">Xml文件全路径</param>
        /// <returns>datatable</returns>
        public static DataTable getDatatable(string xmlfilepath)
        {
            DataSet ds = getDataset(xmlfilepath);
            if (ds == null || ds.Tables.Count < 1)
            {
                return null;
            }
            return ds.Tables[0];
        }

        /// <summary>
        /// 获取 dataset
        /// </summary>
        /// <param name="xmlcontent">Xml内容</param>
        /// <returns>dataset</returns>
        public static DataSet getDatasetBycontent(string xmlcontent)
        {    
            DataSet ds = null;
            try
            {
                StringReader stringReader = new StringReader(xmlcontent);
                XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
                ds = new DataSet();
                ds.ReadXmlSchema(xmlTextReader);
            }
            catch
            {
                return null;
            }

            return ds;
        }

        /// <summary>
        /// 获取 datatable
        /// </summary>
        /// <param name="xmlcontent">Xml内容</param>
        /// <returns>datatable</returns>
        public static DataTable getDatatableBycontent(string xmlcontent)
        {
            DataSet ds = getDatasetBycontent(xmlcontent);
            if (ds == null || ds.Tables.Count < 1)
            {
                return null;
            }

            return ds.Tables[0];
        }

        /// <summary>
        /// 将 xmlcontent 写成 Xml文件
        /// </summary>
        /// <param name="xmlcontent">Xml内容</param>
        /// <returns>保存Xml全路径</returns>
        public static string writeXmlByxmlcontent(string xmlcontent, string savefilepath)
        {
            try
            {
                File.WriteAllText(savefilepath, xmlcontent);
            }
            catch
            {
                return "";
            }
            return savefilepath;
        }

        /// <summary>
        /// 将 dataset 写成 Xml文件
        /// </summary>
        /// <param name="ds">dataset</param>
        /// <param name="savefilepath">保存Xml全路径</param>
        /// <returns>保存Xml全路径</returns>
        public static string writeXmlBydataset(DataSet ds, string savefilepath)
        {
            string[] columns = new string[ds.Tables[0].Columns.Count];
            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                columns[i] = ds.Tables[0].Columns[i].ColumnName;
            }
            return writeXmlBydatatable(ds.Tables[0], savefilepath, columns);
        }

        /// <summary>
        /// 将 datatable 写成 Xml文件
        /// </summary>
        /// <param name="dt">datatable</param>
        /// <param name="savefilepath">保存Xml全路径</param>
        /// <returns>保存Xml全路径</returns>
        public static string writeXmlBydatatable(DataTable dt, string savefilepath,string[] columns)
        {
            try
            {
                System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
                XmlDeclaration xmldecl;
                xmldecl = xmlDoc.CreateXmlDeclaration("1.0", null, null);
                xmldecl.Encoding = "UTF-8";
                xmldecl.Standalone = "yes";
                XmlElement xmlElem = xmlDoc.CreateElement("table");
                xmlDoc.InsertBefore(xmldecl, xmlElem);

                if (columns == null || columns.Length < 1)
                {
                    return "";
                }

                foreach (DataRow row in dt.Rows)
                {
                    XmlElement xmlElemrow = xmlDoc.CreateElement("row");
                    foreach (string column in columns)
                    {
                        XmlElement xmlElemcolum = xmlDoc.CreateElement(column);
                        xmlElemcolum.Value = row[column].ToString().Trim();
                        xmlElemrow.AppendChild(xmlElemcolum);
                    }
                    xmlElem.AppendChild(xmlElemrow);
                }
            }
            catch
            {
                return "";
            }

            return savefilepath;

        }
    }
}
